package xioc;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
/**
 *
 * @author Gailufeng
 */
public class XmlIocLoader {
    static public Map<String, Bean> parse(String file){
          Map<String, Bean> beansMap = new HashMap<String, Bean>();
          Bean bean = new Bean();

          Set<Object> valueSet = null;
          List<Object> valuesList = null;
          Map<String, Object> propertiesMap = null;

          String propertyName = null;
          String key = null;
          String value = null;
          Boolean newValue = false;
     try {
            FileInputStream in = new FileInputStream(file);
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader parser = factory.createXMLStreamReader(in);
            while (true) {
                int event = parser.getEventType();
                switch (event) {
                    case XMLStreamConstants.START_DOCUMENT:
 //                        System.out.print(time);
//                        System.out.println("start document");
//                        System.out.println("schema: "  + parser.getCharacterEncodingScheme());
                        break;
                    case XMLStreamConstants.PROCESSING_INSTRUCTION:
                        break;
                    case XMLStreamConstants.COMMENT:
//                       System.out.println(parser.getText());
                        break;
                    case XMLStreamConstants.START_ELEMENT:
//                      System.out.println("start ele: " + parser.getLocalName());
                           
                            if(parser.getLocalName().equals("bean")){
                                bean = new Bean();
                                int attributeCount = parser.getAttributeCount();
                                for (int i = 0; i < attributeCount; i++) {
                                  if(parser.getAttributeLocalName(i).equals("id")){
                                        bean.setId(parser.getAttributeValue(i));
              System.out.println("id:"+ parser.getAttributeValue(i));
                                  }else if(parser.getAttributeLocalName(i).equals("class")){
                                        bean.setType(parser.getAttributeValue(i));
              System.out.println("class:"+ parser.getAttributeValue(i));
                                  }
                                }
                            }else if(parser.getLocalName().equals("property")){
                                int attributeCount = parser.getAttributeCount();
                                for (int i = 0; i < attributeCount; i++) {
                                    String attribName = parser.getAttributeLocalName(i);
                                    String attribValue = parser.getAttributeValue(i);
                                    if (attribName.equals("name")) {
                                        propertyName = attribValue;
           System.out.println("property name:"+ propertyName);
                                    }else if (attribName.equals("value")) {
                                       bean.getProperties().put(propertyName, attribValue);
           System.out.println("property value:"+ attribValue);
                                    }else if(attribName.equals("ref")){
                                        String[] refString = new String[] { attribValue };
                                        bean.getProperties().put(propertyName, refString);
            System.out.println("property ref:"+ attribValue);
                                    }
                                }
                            }else if(parser.getLocalName().equals("list")){
                                 valuesList = new ArrayList<Object>();
            System.out.println("list:");
                            }else if(parser.getLocalName().equals("map")){
                                 propertiesMap = new HashMap<String, Object>();
             System.out.println("map:");
                            }else if(parser.getLocalName().equals("entry")){
                                 int attributeCount = parser.getAttributeCount();
                                 for (int i = 0; i < attributeCount; i++) {
                                    String attribName = parser.getAttributeLocalName(i);
                                    String attribValue = parser.getAttributeValue(i);
                                    if (attribName.equals("key")) {
                                        key = attribValue;
            System.out.println("key:"+ key);
                                    }
                                 }
                            }else if(parser.getLocalName().equals("value")){
                                 newValue = true;
                            }else if(parser.getLocalName().equals("ref")){
                                int attributeCount = parser.getAttributeCount();
                                for (int i = 0; i < attributeCount; i++) {
                                    String attribName = parser.getAttributeLocalName(i);
                                    String attribValue = parser.getAttributeValue(i);
                                    if (attribName.equals("bean")) {
                                        String[] refString = new String[]{attribValue};
                                        if (valuesList != null) {
                                            valuesList.add(refString);
                                        } else if (propertiesMap != null) {
                                            propertiesMap.put(key, refString);
                                        }
                                    }
                                 }
                            }

                        break;
                    case XMLStreamConstants.CHARACTERS:
                        if (parser.isWhiteSpace())
                             break;
                        if( parser.getText().trim() != null){
                            if(newValue){
                                value = parser.getText().trim();
                                newValue = false;
          System.out.println("value:"+ value);
                            }
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                         if(parser.getLocalName().equals("/bean")){
                                beansMap.put(bean.getId(), bean);
                            }else if(parser.getLocalName().equals("/property")){
                                if(valuesList != null){
                                     bean.getProperties().put(propertyName, valuesList);
                                     valuesList = null;
                                 }else if(propertiesMap != null){
                                    bean.getProperties().put(propertyName, propertiesMap);
                                    propertiesMap = null;
                                 }else if(valueSet != null){
                                    valueSet = null;
                                 }else{
                                     bean.getProperties().put(propertyName, value);
                                 }
                            }else if(parser.getLocalName().equals("/list")){
                                 
                            }else if(parser.getLocalName().equals("/map")){
                                 
                            }else if(parser.getLocalName().equals("/entry")){
                                
                            }else if(parser.getLocalName().equals("/value")){
                                 if(valuesList != null){
                                     valuesList.add(value);
                                 }else if(propertiesMap != null){
                                     propertiesMap.put(key, value);
                                 }else if(valueSet != null){

                                 }else{

                                 }
                            }

                        break;
                    case XMLStreamConstants.END_DOCUMENT:
//                        System.out.println("end document");
                }

                if (parser.hasNext()) {
                    event = parser.next();
                } else {
                    break;
                }
            }
            parser.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("file not found");
        }


        return beansMap;
    }
}
